package com.dgut.service.serviceImpl;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckRole;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dgut.common.ExceptionIdentificationForOrder;
import com.dgut.common.SelectRole;
import com.dgut.common.StateForLegalRecourse;
import com.dgut.common.StateForUser;
import com.dgut.mapper.BuyerMapper;
import com.dgut.mapper.OrderFlagMapper;
import com.dgut.mapper.UserMapper;
import com.dgut.pojo.Buyer;
import com.dgut.pojo.LegalRecourse;
import com.dgut.pojo.OrderFlag;
import com.dgut.pojo.User;
import com.dgut.pojo.receive.AdministratorAndBuyerReceive;
import com.dgut.service.BuyerService;
import io.swagger.models.auth.In;
import com.dgut.utils.PageRequestInfo;
import com.dgut.utils.PageResult;
import com.dgut.utils.PageUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Service
public class BuyerServiceImpl implements BuyerService {
    @Resource
    private BuyerMapper buyerMapper;
    @Resource
    private UserMapper userMapper;
    @Resource
    private OrderFlagMapper orderFlagMapper;
    /**
     *start-zwb
     */
    /**
     *end-zwb
     */

    /**
     *start-hwf
     */
    @Override
    public Buyer selectBuyerById(int id) {
        return buyerMapper.selectById(id);
    }

    @Override
    public boolean register(AdministratorAndBuyerReceive administratorAndBuyerReceive) {
        Map map = new Hashtable();
        map.put("username",administratorAndBuyerReceive.getUsername());
        List<User> list = userMapper.selectByMap(map);
        if(list.size() != 0){
            return false;
        }else{
            User user = new User();
            Buyer buyer = new Buyer();

            user.setRole(SelectRole.BUYER);
            user.setUsername(administratorAndBuyerReceive.getUsername());
            user.setPassword(administratorAndBuyerReceive.getPassword());
            userMapper.insert(user);

            buyer.setAddress(administratorAndBuyerReceive.getAddress());
            buyer.setId(user.getId());
            buyer.setBirthDate(administratorAndBuyerReceive.getBirthDate());
            buyer.setEmail(administratorAndBuyerReceive.getEmail());
            buyer.setHeadPortrait(administratorAndBuyerReceive.getHeadPortrait());
            buyer.setIdCard(administratorAndBuyerReceive.getIdCard());
            buyer.setName(administratorAndBuyerReceive.getName());
            buyer.setNickname(administratorAndBuyerReceive.getNickname());
            buyer.setPhone(administratorAndBuyerReceive.getPhone());
            buyer.setSex(administratorAndBuyerReceive.getSex());
            buyer.setState(StateForUser.NORMAL);
            buyerMapper.insert(buyer);

            return true;
        }
    }

    /**
     *end-hwf
     */


    /**
     * start-lzh
     */
    //采买方查看账户的基本信息
    @Override
    @SaCheckLogin
    @SaCheckRole(SelectRole.BUYER)
    public Object CheckPersonalInformation(Integer id){
        ConcurrentHashMap resultMap = new ConcurrentHashMap();
        Buyer buyer = buyerMapper.selectById(id);
        resultMap.put("buyerInformation",buyer);
        return resultMap;
    }

    //采买方修改账户的基本信息
    @Override
    @SaCheckLogin
    @SaCheckRole(SelectRole.BUYER)
    public Object UpdatePersonalInformation(Buyer buyer){
        ConcurrentHashMap resultMap = new ConcurrentHashMap();
        int update = buyerMapper.updateById(buyer);
        if (update>=1){
            resultMap.put("message","更新成功！");
            resultMap.put("result",true);
        }
        else {
            resultMap.put("result",false);
        }
        return resultMap;
    }
    
    /**
     * end-lzh
     */


    /**
     * start-gxd
     */

    @Override
    public Object getBuyerByPage(PageRequestInfo pageRequestInfo) {
        ConcurrentHashMap resultMap = new ConcurrentHashMap();
        Page<Buyer> page = new Page(pageRequestInfo.getPageNum(), pageRequestInfo.getPageSize());
        Page<Buyer> resultPage = buyerMapper
                .selectPage(page,null);
        PageResult pageResult = PageUtils.getPageResult(resultPage);
        if (pageResult != null){
            resultMap.put("pageResult",pageResult);
            resultMap.put("result",true);
            resultMap.put("message","查询成功！");
        } else {
            resultMap.put("result",false);
            resultMap.put("message","查询失败！");
        }
        return resultMap;
    }

    @Override
    public Object selectBuyerListByName(String name){
        ConcurrentHashMap resultMap = new ConcurrentHashMap();
        QueryWrapper<Buyer> wrapper = new QueryWrapper<>();
        if (name == null){
            List<Buyer> list = buyerMapper.selectList(null);
            if(list != null){
                resultMap.put("list",list);
                resultMap.put("result",true);
                resultMap.put("message","查询成功！");
            }   else {
                resultMap.put("result", false);
                resultMap.put("message", "查询失败！");
            }
        }
        else {
            wrapper.like("name", name);
            List<Buyer> list = buyerMapper.selectList(wrapper);
            if(list != null){
                resultMap.put("list",list);
                resultMap.put("result",true);
                resultMap.put("message","查询成功！");
            }   else {
                resultMap.put("result", false);
                resultMap.put("message", "查询失败！");
            }
        }
        return resultMap;
    }

    @Override
    public Object selectBuyerById1(int id){
        ConcurrentHashMap resultMap = new ConcurrentHashMap();
        Buyer buyer = buyerMapper.selectById(id);
        if (buyer != null){
            resultMap.put("buyer",buyer);
            resultMap.put("result",true);
            resultMap.put("message","查询成功！");
        } else {
            resultMap.put("result",false);
            resultMap.put("message","查询失败！");
        }
        return resultMap;
    }
    /**
     * end-gxd
     */

}
